package org.linlinjava.litemall.wxserver.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallMechanismGoods;
import org.linlinjava.litemall.db.domain.LitemallMechanismVo;
import org.linlinjava.litemall.db.service.LitemallMechanismGoodsCategoryService;
import org.linlinjava.litemall.db.service.LitemallMechanismGoodsService;
import org.linlinjava.litemall.db.service.LitemallMechanismService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 获取附近的代金券
 *
 * @author zhy
 * @create 2018-12-20 14:20
 **/
@RestController
@RequestMapping("/wxs/ticket")
public class WxsNearbyTicketController {
	private final Log logger = LogFactory.getLog(WxsNearbyTicketController.class);

	@Autowired
	private LitemallMechanismService litemallMechanismService;
	@Autowired
	private LitemallMechanismGoodsService litemallMechanismGoodsService;

	/**
	 * 通过地理位置获取用户周边的商户及推荐的代金券
	 * @return
	 */
    @GetMapping("/getTicket")
	public Object getNearbyTicket(String category,String longitude,String latitude){
	    LocalDateTime time = LocalDateTime.now();
    // 通过参数查询附近的构(查询)
//	    longitude = "118.768250";
//	    latitude = "31.986480";
//	    category = 100017+"";
		List<LitemallMechanismVo> mechanismList = litemallMechanismService.queryByLocate(category,longitude,latitude);
	// 机构id
        List<Integer> mechanismIds = mechanismList.stream().map(LitemallMechanismVo::getId).collect(Collectors.toList());
    // 分类下的商品(查询)
	    List<LitemallMechanismGoods> mechanismGoodsList = null;
	    if(mechanismIds.size()>0){
		    mechanismGoodsList = litemallMechanismGoodsService.getAllGoods(mechanismIds,time);
	    }

	    Map<String,Object> map = new HashMap<>(16);
	    map.put("ticket",mechanismGoodsList);
	    map.put("mechanism",mechanismList);
	    return ResponseUtil.ok(map);
	}
}
